﻿$.MGJ.ApplyCode = [];

(function () {
    var _this = $.MGJ.ApplyCode;
    $.extend($.MGJ.ApplyCode, {
        DataModel: {},
        //码申请新增
        Dialog_ApplyCodeObj: null,
        Dialog_ApplyCode: function (callback) {
            _this.Dialog_ApplyCodeObj = $.L.dialog({
                url: ACTION_URL("ApplyCodeAdd", "CodeManage"),
                title: "码申请新增",
                width: 800,
                height: "auto",
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            var bootstrapValidator = $("#form_ApplyCodeAdd").data('bootstrapValidator').validate();
                            if (bootstrapValidator.isValid()) {
                                $.MGJ.ApplyCode.Ajax_AddApply();
                                $(".btn-primary").attr("disabled", true);
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            };
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },
        arr: [1, 2],
        Dialog_View: function (id) {
            _this.DataModel = _this.ReturnModelById(id);         
           arr = _this.DataModel.CodeFunc.split(',');
   
            if (!_this.DataModel) {
                $.L.msgWarning("当前无法执行该操作,刷新后重试");
                return;
            }
            $.L.dialog({
                url: ACTION_URL("ApplyCodeView", "CodeManage"),
                title: "码申请查看",
                width: 800,
                height: "auto",
                buttons: {
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });

            setTimeout(function () {
                if (arr.length == 1) {
                    if (arr[0] == 1)
                    { $('.CodeTypes[value=1]').attr('checked', true) }
                    else if (arr[0] == 2) {
                        $('.CodeTypes[value=2]').attr('checked', true)
                    }
                }
                else {
                    $('.CodeTypes').attr('checked', true)
                }

            }, 1000)
        },
    });

    

})(jQuery);
(function () {
    var _this = $.MGJ.ApplyCode;
    $.extend($.MGJ.ApplyCode, {
        //多条件查询
        ApplyPageList:[],
        PageIndex:1,
        Ajax_GetApplyList: function (pageIndex) {
            PageIndex = pageIndex || 1;
            window.SearchKey = removeSpace($("#SerialNum").val());
            $.L.ajaxHandle({
                url: ACTION_URL("GetApplyList", "CodeManage") + "?pageindex=" + PageIndex,
                data: $("#SearchForm").serialize(),
                success: function (response) {
                    _this.ApplyPageList = response.PageData;
                    $("#ApplyList").L_bindingDataList(response);
                    $(".AllotProductName").each(function () {
                        if ($(this).text() == "--请选择--") {
                            $(this).html("")
                        }
                    })
                }
            });
        },
        //添加
        Ajax_AddApply: function (EnterpriseId) {
            var ProductName = $("#ProductId").find('option:selected').text();
            var ProductId = $("#ProductId").val();           
            var ApplyTypes = [];
            $('input[name="ApplyCodeTypes"]:checked').each(function (i, e) {
                ApplyTypes.push($(this).val())
            });
            //function fun() {
            //    obj = document.getElementsByName("test");
            //    check_val = [];
            //    for (k in obj) {
            //        if (obj[k].checked)
            //            check_val.push(obj[k].value);
            //    }
            //    alert(check_val);
            //}
         
            //var EnterpriseId = $("#EnterpriseId").val();
            //var ProductId = $("#ProductId").find('option:selected').text();
            //var ProductName = $("#ProductId").val();
            //var _dataModel = {
            //   
            //    F_EeterpriseName: $("#EnterpriseId").find('option:selected').text(),
            //    ProductId: $("#ProductId").val(),
            //    AllotProductName: $("#ProductId").find('option:selected').text(),
            //    ApplyReason: $("#ApplyReason").val(),
            //    AllotCount: $("#AllotCount").val(),
            //    ApplyProductSize: $("#ApplyProductSize").val(),
            //    ApplyUserName: $("#ApplyUserName").val(),
            //    ApplyCodeType: $('input[name="ApplyCodeTypes"]:checked').each(function (i, e) {
            //        ApplyCodeTypes.push($(this).val())
            //    })
            //};
            $("#CodeFunc").val(ApplyTypes.join(','));
           var F_EnterpriseId = $("#EnterpriseId").val();
            
            $.L.ajaxHandle({
                url: ACTION_URL("AddApply", "CodeManage") + "?F_EnterpriseId=" + F_EnterpriseId + "&AllotProductName=" + ProductName + "&ProductId=" + ProductId,
                data: $("#form_ApplyCodeAdd").serialize(),

                success: function (response) {
                    if (response > 0) {
                        _this.Dialog_ApplyCodeObj.close();
                        $.L.msgSuccess("操作成功！");
                        _this.Ajax_GetApplyList(1);
                    }
                    else {
                        $.L.msgError("操作失败！");
                    }
                }
            });
        },
        Ajax_handers: function (obj) {
            var temp = obj.split(",");
            var result = "";
            for (i = 0; i < temp.length ; i++) {
                if (temp[i] == 1) {
                    result += " 服务码 ";
                } else if (temp[i] == 2) {
                    result += " 管理码 ";
                } 
            }
            return result;
        },
        //取消
        CancelApply: function (a) {
            if (a) {
                $.L.msgConfirm("确认取消该条申请?", function () {
                    $.L.ajaxHandle({
                        url: ACTION_URL("CancelApply", "CodeManage"),
                        type: "GET",
                        data: { id: a },
                        success: function (r) {
                            _this.Ajax_GetApplyList(_this.PageIndex);
                            $.L.msgSuccess("取消成功");
                        }
                    })
                })
            }
        }
    });
})(jQuery);
(function () {
    var _this = $.MGJ.ApplyCode;
    $.extend($.MGJ.ApplyCode, {
        //返回审核状态
        ReturnVerifieState: function (a) {
            if (0 == a) {
                return "末审核";
            }
            if (1 == a) {
                return "审核通过";
            }
            if (2 == a) {
                return "审核驳回";
            }
            return "----";
        },
        //返回显示按钮
        ReturnOpButton: function (a, b, c) {
            if (0 == a) {
                c[0].innerHTML = "<a  onclick='$.MGJ.ApplyCode.CancelApply(" + b.ID + ")'>取消</a>";
            } 
            c[0].innerHTML += "<a style='margin-left:5px;' onclick='$.MGJ.ApplyCode.Dialog_View(" + b.ID + ")'>查看</a>"
        },
        //通过ID寻找实体模型
        ReturnModelById: function (id) {
            if (!_this.ApplyPageList || !id) {
                return null;
            }
            if (_this.ApplyPageList instanceof Array) {
                for (var item in _this.ApplyPageList) {
                    var _temp = _this.ApplyPageList[item];
                    if (_temp && _temp.ID === id) {
                        return _temp;
                    }
                }
            }
            return null;
        }
    });
})(jQuery);